package model.impl;

import java.util.List;

import model.bo.Sub;
import model.dao.ISubDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class Sub.
 * 
 * @see model.bo.Sub
 * @author MyEclipse Persistence Tools
 */

public class SubDAO extends HibernateDaoSupport implements ISubDAO {
	private static final Log log = LogFactory.getLog(SubDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#save(model.bo.Sub)
	 */
	public void save(Sub transientInstance) {
		log.debug("saving Sub instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#delete(model.bo.Sub)
	 */
	public void delete(Sub persistentInstance) {
		log.debug("deleting Sub instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findById(java.lang.Integer)
	 */
	public Sub findById(java.lang.Integer id) {
		log.debug("getting Sub instance with id: " + id);
		try {
			Sub instance = (Sub) getHibernateTemplate().get("model.bo.Sub", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findByExample(model.bo.Sub)
	 */
	public List findByExample(Sub instance) {
		log.debug("finding Sub instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Sub instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Sub as model where model."
					+ propertyName + "= ? order by sn asc";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findBySubname(java.lang.Object)
	 */
	public List findBySubname(Object subname) {
		return findByProperty(SUBNAME, subname);
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findByFatherid(java.lang.Object)
	 */
	public List findByFatherid(Object fatherid) {
		return findByProperty(FATHERID, fatherid);
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findBySn(java.lang.Object)
	 */
	public List findBySn(Object sn) {
		return findByProperty(SN, sn);
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Sub instances");
		try {
			String queryString = "from Sub";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#merge(model.bo.Sub)
	 */
	public Sub merge(Sub detachedInstance) {
		log.debug("merging Sub instance");
		try {
			Sub result = (Sub) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#attachDirty(model.bo.Sub)
	 */
	public void attachDirty(Sub instance) {
		log.debug("attaching dirty Sub instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.bo.ISubDAO#attachClean(model.bo.Sub)
	 */
	public void attachClean(Sub instance) {
		log.debug("attaching clean Sub instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ISubDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ISubDAO) ctx.getBean("SubDAO");
	}
}